package com.zkx.mvp.utils;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;

import com.zkx.mvp.MainActivity;

import java.util.Stack;

/**
 * Author：zhangkx on 2017/6/28 16:07
 * Email：zkx9025@163.com
 * acivity 统一管理类
 */

public class AppManager {
    private static boolean isAppExit=false;
    private static Stack<Activity> activityStack;
    private static AppManager instance;
    private AppManager(){}
    /**
     * 单一实例
     */
    public static AppManager getAppManager(){
        if(instance==null){
            instance=new AppManager();
        }
        return instance;
    }
    /**
     * 添加Activity到堆
     */
    public void addActivity(Activity activity){
        if(activityStack==null){
            activityStack=new Stack<Activity>();
        }
        activityStack.add(activity);
    }
    /**
     * 获取当前Activity（堆栈中最后压入栈的activity）
     */
    public Activity currentActivity(){
        Activity activity=activityStack.lastElement();
        return activity;
    }
    /**
     * 结束当前Activity（堆栈中最后压入栈的activity）
     */
    public void finishActivity(){
        Activity activity=activityStack.lastElement();
        finishActivity(activity);
    }
    /**
     * 结束指定的Activity
     */
    public void finishActivity(Activity activity){
        if(activity!=null){
            activityStack.remove(activity);
            activity.finish();
            activity=null;
        }
    }
    /**
     * 结束指定类名的Activity
     */
    public void finishActivity(Class<?> cls){
        for (Activity activity : activityStack) {
            if(activity.getClass().equals(cls) ){
                finishActivity(activity);
                return;
            }
        }
    }
    /**
     * 结束堆栈中所有的Activity
     */
    public void finishAllActivity(){
        for (int i = 0, size = activityStack.size(); i < size; i++){
            if (null != activityStack.get(i)){
                activityStack.get(i).finish();
            }
        }
        activityStack.clear();
    }


    /**
     * 结束所有Activity
     */
    public void finishAllActivityNoMain(){
        for (int i = 0, size = activityStack.size(); i < size; i++){
            if (null != activityStack.get(i)){
                if (!activityStack.get(i).getClass().equals(MainActivity.class)) {
                    activityStack.get(i).finish();
                }
            }
        }
        activityStack.clear();
    }

    /**
     * 判断activity是否在栈中
     */

    public boolean isHaveActivity(Class<?> cls) {
        boolean isHave = false;
        for (Activity activity : activityStack) {
            if (activity.getClass().equals(cls)) {
                isHave = true;
            }
        }
        LogUtils.i("AppManager", "cls ---- " + isHave + "----" + cls.getName());
        return isHave;
    }
    /**
     * 退出应用程序
     */
    public void AppExit(Context context) {
        try {
            isAppExit=true;
            finishAllActivity();
            ActivityManager activityMgr= (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            activityMgr.restartPackage(context.getPackageName());
            System.exit(0);
            android.os.Process.killProcess(android.os.Process.myPid());
        } catch (Exception e) {	}
    }
    public static boolean isAppExit() {
        return isAppExit;
    }
    public static void setAppExit(boolean isAppExit) {
        AppManager.isAppExit = isAppExit;
    }
}
